package com.atguigu.srb.core.controller.admin;


import com.alibaba.excel.EasyExcel;
import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.dto.ExcelDictDTO;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author oldCookies
 * @since 2021-09-12
 */
@Api(tags="积分等级管理")
@RestController
//@CrossOrigin
@Slf4j
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;

    DictService dictService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R list(){
        log.info("hi i'm helen");
        log.warn("warning!!!");
        log.error("it's a error");
     return R.ok().data("data",integralGradeService.list());
    }


    @ApiOperation(value = "删除积分等级列表",notes = "fdasfdsa")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable
           @ApiParam(value = "id",required = true,example = "100") Long id){
        boolean b = integralGradeService.removeById(id);
        if (b){
            return R.ok();
        }else {return R.error();}
    }

    @ApiOperation(value = "新增积分等级")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade) {

//        if (integralGrade.getBorrowAmount()==null){
//            throw  new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean save = integralGradeService.save(integralGrade);
        if (save){
            return R.ok().Message("保持成功");
        }else {
            return R.error().Message("保持失败");
        }
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable Long id
    ){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if(integralGrade != null){
            return R.ok().data("record", integralGrade);
        }else{
            return R.error().Message("数据不存在");
        }
    }

    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade){
        boolean result = integralGradeService.updateById(integralGrade);
        if(result){
            return R.ok().Message("修改成功");
        }else{
            return R.error().Message("修改失败");
        }
    }



}

